package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/dish/page")
    public ResultInfo findPage(
           @RequestParam(value = "page" ,defaultValue = "1") Integer pageNum,
           @RequestParam(value = "pageSize" ,defaultValue = "10") Integer pageSize,
            String name){
       //调用方法
        Page<Dish> page = dishService.findPage(pageNum,pageSize,name);

        return ResultInfo.success(page);
    }

    //根据id获取菜

    @GetMapping("/dish/{id}")
    public ResultInfo queryDishById(@PathVariable("id")Long id){
        Dish dish = dishService.queryDishById(id);
        return ResultInfo.success(dish);
    }



    //新增菜品
    @PostMapping("/dish")
    public ResultInfo saveDish(@RequestBody Dish dish){
        //删除缓存中的数据
        redisTemplate.opsForValue().decrement("dish_" + dish.getCategoryId());
        dishService.saveDish(dish);
        return ResultInfo.success(null);
    }

    //更改菜品
    @PutMapping("/dish")
    public ResultInfo editDish(@RequestBody Dish dish){
        redisTemplate.opsForValue().decrement("dish_*") ;
        dishService.editDish(dish);
        return ResultInfo.success(null);
    }

    //删除
    @DeleteMapping("/dish")
    public ResultInfo delete(Long[] ids){
        redisTemplate.opsForValue().decrement("dish_*") ;
        dishService.delete(ids);
        return ResultInfo.success(null);
    }

    //禁售起售
    @PostMapping("/dish/status/{status}")
    public ResultInfo statusChange(@PathVariable("status") Integer status ,Long[] ids){
        redisTemplate.opsForValue().decrement("dish_*") ;
        dishService.statusChange(status,ids);
        return ResultInfo.success(null);
    }




    //通过分类id查找相应对菜
    @GetMapping("/dish/list")
    public ResultInfo findDish(String name,Long categoryId,@RequestParam(defaultValue = "1") Integer status){
        //调用方法
        List<Dish> dishList = dishService.findDishList(name,categoryId,status);

        return ResultInfo.success(dishList);
    }
}
